$(function () {
    getMyWorkes();
    getMyClasses();
    var locale = {
        "format": 'YYYY/MM/DD',
        "separator": " - ",
        "applyLabel": "确定",
        "cancelLabel": "取消",
        "fromLabel": "起始时间",
        "toLabel": "结束时间'",
        "customRangeLabel": "自定义",
        "weekLabel": "W",
        "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
        "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        "firstDay": 1
    };
    $('#subTime').datepicker({
        'locale': locale,
        ranges: {
            '今日': [moment(), moment()],
            '明日': [moment(), moment().subtract(-1, 'days')],
            '一周': [moment(), moment().subtract(-6, 'days')],
            '本月': [moment().startOf('month'), moment().endOf('month')]
        },
        "alwaysShowCalendars": true,
        "startDate": new Date(),
        "endDate": new Date(),
        "opens": "right",
    }, function (start, end, label) {
        console.log('New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') +
            ' (predefined range: ' + label + ')');
    });
    // $('#editer').summernote({
    //     height: 350
    // });
    var E = window.wangEditor
    var editor2 = new E('#editer')
    editor2.create()
    $("#className").change(function () {
        var checkValue = $("#className").val(); //获取Select选择的Value
        if (checkValue != "") {
            // alert(checkValue)
        }

    })
    $("#btn-publish-work").click(function () {
        var className = $("#className").val();
        var workName = $("#workName").val();
        var subTime = $("#subTime").val();
        var editer = editor2.txt.html()

        console.log(className);
        console.log(workName);
        console.log(subTime);
        console.log(editer);
        if (workName.length > 0) {
            $.ajax({
                type: "POST",
                dataType: "json",
                // contentType: "application/json;charset=UTF-8",
                // processData: false,
                // contentType: false,
                url: "/teachers/taddwork",
                data: {
                    classId: $("#className").val(),
                    workName: workName,
                    subTime: subTime,
                    workDesc: editer
                },
                success: function (e) {
                    if (e) {
                        $('#works-table').DataTable().ajax.reload();
                        workName = "";
                        editer = "";
                        className = "";
                        $("#addModal").modal('hide');
                    }
                }
            });
        }


    })


})

function getMyClasses() {
    var select_node = $("#className")
    select_node.empty();
    $.ajax({
        type: "GET",
        dataType: "json",
        // contentType: "application/json;charset=UTF-8",
        url: "/teachers/tmyclasses",
        success: function (result) {
            console.log(result)
            $.each(result, function (index, dataItem) {
                select_node.append("<option value='" + dataItem.id + "'>" + dataItem.className + "</option>");
            });
            // if (data.flag) {
            //     $("#addModal").modal('hide');
            //
            // } else {
            // $("#name_msg").html(data.msg)
            // }
        }
    });
}

var lang = {
    "sProcessing": "处理中...",
    "sLengthMenu": "每页 _MENU_ 项",
    "sZeroRecords": "没有匹配结果",
    "sInfo": "当前显示第 _START_ 至 _END_ 项，共 _TOTAL_ 项。",
    "sInfoEmpty": "当前显示第 0 至 0 项，共 0 项",
    "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
    "sInfoPostFix": "",
    "sSearch": "搜索:",
    "sUrl": "",
    "sEmptyTable": "表中数据为空",
    "sLoadingRecords": "载入中...",
    "sInfoThousands": ",",
    "oPaginate": {
        "sFirst": "首页",
        "sPrevious": "上页",
        "sNext": "下页",
        "sLast": "末页",
        "sJump": "跳转"
    },
    "oAria": {
        "sSortAscending": ": 以升序排列此列",
        "sSortDescending": ": 以降序排列此列"
    }
};


function getMyWorkes() {
    var table = $('#works-table').DataTable({
        language: lang,
        lengthMenu: [
            5, 10, 15, 30, 50
        ],
        // processing: true,
        // serverSide: true,
        // bInfo: false, //是否显示是否启用底边信息栏
        ajax: {
            url: "/teachers/tkeworkkist",
            type: 'POST',
            dataSrc: function (e) {
                console.log(e)
                return e
            }
        },
        // data:data,
        autoWidth: true,
        // pagingType: 'full_numbers',
        columns: [
            {data: "keName"},
            {data: "workName"},
            {data: "subStart"},
            {data: "subEnd"},
            {
                data: "pubSta",
                render: function (data, type, row, meta) {
                    console.log(data)
                    console.log(typeof data)
                    if (data == 0) {
                        return data = '<span class="btn btn-danger btn-sm">未发放</span>'
                    } else {
                        return data = '<span class="btn btn-success btn-sm">已发放</span>'
                    }

                }
            },
            {data: "createTime"},
            {
                data: "id",
                orderable: false, // 禁用排序
                defaultContent: "",
                // width: "10%",
                render: function (data, type, row, meta) {
                    return data = '<a class="btn btn-default btn-sm" id="edit"><i class="fa fa-pencil"></i>下发作业</a>  ' +
                        '<a class="btn  btn-danger btn-sm" id="del"><i class="fa fa-trash-o"></i>批改作业</a>  ' +
                        '<a class="btn btn-default btn-sm" id="edit"><i class="fa fa-pencil"></i>删除</a>'
                }
            }
        ]
    });
}